package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.CsChangeRegulationMapper;
import cn.tedu.eb_film.pojo.dto.CsChangeRegulationDto;
import cn.tedu.eb_film.pojo.dto.CsChangeRegulationUpdateDto;
import cn.tedu.eb_film.pojo.entity.CsChangeRegulation;
import cn.tedu.eb_film.pojo.entity.CsRevocationRegulation;
import cn.tedu.eb_film.pojo.vo.CsChangeRegulationVo;
import cn.tedu.eb_film.pojo.vo.CsRevocationRegulationStandardVO;
import cn.tedu.eb_film.service.CsChangeRegulationService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class CsChangeRegulationServiceImp implements CsChangeRegulationService {
    @Autowired
    CsChangeRegulationMapper csChangeRegulationMapper;

    /**
     * 插入单个改签标准
     * @param csChangeRegulationDto
     */
    @Override
    public void AddNewChangeRegulation(CsChangeRegulationDto csChangeRegulationDto) {

        CsChangeRegulation csChangeRegulation = new CsChangeRegulation();
        BeanUtils.copyProperties(csChangeRegulationDto,csChangeRegulation);
        csChangeRegulation.setCreateTime(LocalDateTime.now());
        csChangeRegulationMapper.AddNewChangeRegulation(csChangeRegulation);
    }

    /**
     * 批量插入改签标准
     * @param list
     */
    @Override
    public void AddNewChangeRegulationList(List<CsChangeRegulation> list) {
        csChangeRegulationMapper.AddNewChangeRegulationList(list);
    }

    /**
     * 根据id删除改签标准
     * @param id
     */
    @Override
    public void deleteChangeRegulationById(Long id) {
        csChangeRegulationMapper.deleteChangeRegulationById(id);
    }

    /**
     * 根据ids集合批量删除
     * @param list
     */
    @Override
    public void deleteChangeRegulationByIds(List<Long> list) {
        csChangeRegulationMapper.deleteChangeRegulationByIds(list);
    }

    /**
     * 统计改签标准数量
     */
    @Override
    public void countChangeRegulation() {
        csChangeRegulationMapper.countChangeRegulation();
    }

    /**
     * 通过id获取改签标准
     * @param id
     * @return
     */
    @Override
    public CsChangeRegulationVo getChangeRegulationById(Integer id) {
        return csChangeRegulationMapper.getChangeRegulationById(id);
    }

    /**
     * 获取改签标准计集合
     * @return
     */
    @Override
    public List<CsChangeRegulationVo> getChangeRegulationList() {
        return csChangeRegulationMapper.getChangeRegulationList();
    }

    /**
     * 修改改签标准
     * @param csChangeRegulationUpdateDto
     */
    @Override
    public void updateChangeRegulation(CsChangeRegulationUpdateDto csChangeRegulationUpdateDto) {

        csChangeRegulationMapper.updateChangeRegulation(csChangeRegulationUpdateDto);
    }

    @Override
    public List<CsChangeRegulationVo> search(String connect) {
        List<CsChangeRegulationVo> list = csChangeRegulationMapper.search(connect);
        return list;
    }

    @Override
    public void setEnable(Integer id) {
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Integer id) {
        updateEnableById(id,0);
    }

    private void updateEnableById(Integer id,Integer enable) {
        CsChangeRegulationVo queryResult = csChangeRegulationMapper.getChangeRegulationById(id);
        if (queryResult == null || queryResult.getState().equals(StateCode.STATE_DELETE.getValue())) {
            String message = ENABLE_TEXT[enable] + "数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (enable == queryResult.getState()) {
            String message = ENABLE_TEXT[enable] + "失败,修改的目标状态与目前一致";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        CsChangeRegulationUpdateDto csChangeRegulationUpdateDto = new CsChangeRegulationUpdateDto();
        csChangeRegulationUpdateDto.setId(id);
        csChangeRegulationUpdateDto.setState(enable);

        Integer rows = csChangeRegulationMapper.update(csChangeRegulationUpdateDto);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
